<?php

namespace app\company\controller\consult\config;

use app\BaseController;
use app\model\ConsultOfflineModel;
use app\model\ConsultOnlineModel;
use app\model\ConsultQuestionModel;
use app\model\RatingScaleTypeModel;
use app\model\UserModel;
use think\App;

class Index extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 问题类型选择器
     * @return bool|string
     */
    public function questionSelector(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'unit_id|单位ID'  =>  'require',
            ]);
            $where = [];
            $where[] = ['status', '=', 1];
            $where[] = ['unit_id','=',$param['unit_id']];
            $ConsultQuestionModel = new ConsultQuestionModel();
            $data = $ConsultQuestionModel->field('id,name')->where($where)->order('id desc')->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 线上咨询记录-咨询师选择器
     * @return bool|string
     */
    public function ConsultOnlineSelector(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'unit_id|单位ID'  =>  'require',
            ]);
            $ConsultOnlineModel = new ConsultOnlineModel();
            $ConsultIds = $ConsultOnlineModel->where('unit_id','=',$param['unit_id'])->column('consult_id');
            if (!$ConsultIds) return success();
            $ConsultIds = array_unique($ConsultIds);
            $UserModel = new UserModel();
            $data = $UserModel->where('id', 'in', $ConsultIds)->field('id,name')->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 线下预约记录-咨询师选择器
     * @return bool|string
     */
    public function ConsultOfflineSelector(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'unit_id|单位ID'  =>  'require',
            ]);
            $ConsultOfflineModel = new ConsultOfflineModel();
            $ConsultIds = $ConsultOfflineModel->where('unit_id','=',$param['unit_id'])->column('consult_id');
            if (!$ConsultIds) return success();
            $ConsultIds = array_unique($ConsultIds);
            $UserModel = new UserModel();
            $data = $UserModel->where('id', 'in', $ConsultIds)->field('id,name')->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 量表选择器
     * @return bool|string
     */
    public function ratingScaleSelector(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'unit_id|单位ID'  =>  'require',
            ]);
            $where = [];
            $RatingScaleWhere = [];
            if (!empty($param['name'])) {
                $RatingScaleWhere[] = ['name', 'like', '%' . $param['name'] . '%'];
            }
            $where[] = ['unit_id', '=', $param['unit_id']];
            $where[] = ['status', '=', 1];
            $RatingScaleTypeModel = new RatingScaleTypeModel();
            $list = $RatingScaleTypeModel->field('id,name')
                ->where($where)
                ->with([
                    'RatingScale' => function ($query) use ($RatingScaleWhere) {
                        $query->where($RatingScaleWhere)->field('id,type_id,name,mode_id');
                    }
                ])->select()->toArray();
            return success($list);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }
}